/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.PrinterTask;
import me.zhengjie.modules.biz.repository.SettingsRepository;
import me.zhengjie.modules.biz.service.PrinterTaskService;
import me.zhengjie.modules.biz.service.dto.PrinterTaskQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author wanghaonan
* @date 2023-03-27
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "printer-task管理")
@RequestMapping("/api/printerTask")
public class PrinterTaskController {

    private final PrinterTaskService printerTaskService;
    private final SettingsRepository settingsRepository;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('printerTask:list')")
    public void exportPrinterTask(HttpServletResponse response, PrinterTaskQueryCriteria criteria) throws IOException {
        printerTaskService.download(printerTaskService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询printer-task")
    @ApiOperation("查询printer-task")
    @AnonymousAccess
    public ResponseEntity<Object> queryPrinterTask(PrinterTaskQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(printerTaskService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/query")
    @Log("新增printer-task")
    @ApiOperation("新增printer-task打印任务")
    @AnonymousAccess
    public ResponseEntity<Object> createPrinterTask(@RequestParam Integer itemType, @RequestParam Integer itemId, @RequestParam Integer codeType,HttpServletRequest request){
        printerTaskService.create(itemType, itemId, codeType, request);//itemId:425   itemType:3   codeType:1
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改printer-task")
    @ApiOperation("修改printer-task")
//    @PreAuthorize("@el.check('printerTask:edit')")
    public ResponseEntity<Object> updatePrinterTask(@Validated @RequestBody PrinterTask resources){
        printerTaskService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除printer-task")
    @ApiOperation("删除printer-task")
//    @PreAuthorize("@el.check('printerTask:del')")
    public ResponseEntity<Object> deletePrinterTask(@RequestBody Integer[] ids) {
        printerTaskService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/batchCreate")
    @Log("批量新增printer-task")
    @ApiOperation("批量新增printer-task")
    @AnonymousAccess
    public ResponseEntity<Object> createPrinterTasks(@RequestParam Integer itemType, @RequestParam int[] itemIds, @RequestParam Integer codeType, HttpServletRequest request){

        return new ResponseEntity<>(printerTaskService.createTasks(itemType, itemIds, codeType,request), HttpStatus.CREATED);
    }

    @GetMapping("/complete")
    @Log("删除printer-task")
    @ApiOperation("删除printer-task")
    @AnonymousAccess
    public ResponseEntity<Object> completePrinterTask( Integer id){
        printerTaskService.complete(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/list")
    @Log("查询printer-task")
    @ApiOperation("查询printer-task")
    @AnonymousAccess
    public ResponseEntity<Object> list(HttpServletRequest request){
        return new ResponseEntity<>(printerTaskService.queryAllList(request),HttpStatus.OK);

    }
}
